home *** CD-ROM | disk | FTP | other *** search
- package icontrols.MaskedEdit;
-
- public class Mask {
- public static final int LEFT = 1;
- public static final int RIGHT = 2;
- public static final char DIGIT_REQUIRED = '#';
- public static final char CHARACTER = '&';
- public static final char ALPHANUMERIC_REQUIRED = 'A';
- public static final char ALPHANUMERICSPACE = 'a';
- public static final char ALPHA = '?';
- public static final char DIGITPLUSMINUSSPACE = '9';
- public static final char FORCE_UPPER = '>';
- public static final char FORCE_LOWER = '<';
- public static final char ESCAPE = '\\';
- private static final char LITERAL = 'L';
- private static final char NOCASE = '?';
- private static final char UPPER = 'U';
- private static final char LOWER = 'L';
- private int len;
- private String mask;
- private char[] maskEmpty;
- private char[] maskCompiled;
- private char[] maskCase;
- private char promptChar;
-
- public boolean isMetaChar(int index) throws IllegalArgumentException {
- if (index >= 0 && index < this.length()) {
- return this.maskCompiled[index] != 'L';
- } else {
- throw new IllegalArgumentException("Illegal Index");
- }
- }
-
- public boolean isValidCharacter(int index, char c, boolean ignorePrompt) throws IllegalArgumentException {
- if (index >= 0 && index < this.length()) {
- if (ignorePrompt && this.isequalPromptChar(c)) {
- return true;
- } else {
- switch (this.maskCompiled[index]) {
- case '#':
- return Character.isDigit(c);
- case '&':
- return true;
- case '9':
- return this.isequalPromptChar(c) || Character.isDigit(c) || c == '+' || c == '-' || c == ' ';
- case '?':
- return this.isequalPromptChar(c) || Character.isLetter(c);
- case 'A':
- return Character.isLetterOrDigit(c);
- case 'L':
- if (this.maskEmpty[index] != c) {
- return false;
- }
-
- return true;
- case 'a':
- return this.isequalPromptChar(c) || Character.isLetterOrDigit(c) || c == ' ';
- default:
- return false;
- }
- }
- } else {
- throw new IllegalArgumentException("Illegal Index");
- }
- }
-
- public char checkCase(int index, char c) throws IllegalArgumentException {
- if (index >= 0 && index < this.length()) {
- switch (this.maskCase[index]) {
- case '?':
- default:
- break;
- case 'L':
- c = Character.toLowerCase(c);
- break;
- case 'U':
- c = Character.toUpperCase(c);
- }
-
- return c;
- } else {
- throw new IllegalArgumentException("Illegal Index");
- }
- }
-
- public String getEmptyMask() {
- return new String(this.maskEmpty, 0, this.length());
- }
-
- public Mask() {
- this("", '_');
- }
-
- public Mask(String mask) {
- this(mask, '_');
- }
-
- public Mask(String mask, char promptChar) {
- this.len = 0;
- this.mask = mask;
- this.setPromptChar(promptChar);
- }
-
- public boolean isMaskSet() {
- return this.length() != 0;
- }
-
- public String toString() {
- return "{mask=\"" + this.mask + "\",promptChar='" + this.promptChar + "'}";
- }
-
- public char getPromptChar() {
- return this.promptChar;
- }
-
- public boolean isequalPromptChar(char test) {
- return test == this.getPromptChar();
- }
-
- public void setPromptChar(char promptChar) {
- if (promptChar == 0) {
- this.promptChar = ' ';
- } else {
- this.promptChar = promptChar;
- }
-
- this.maskCompile();
- }
-
- private void maskCompile() {
- this.maskCompiled = new char[this.mask.length()];
- this.maskCase = new char[this.mask.length()];
- this.maskEmpty = new char[this.mask.length()];
- this.len = 0;
- char caseState = '?';
-
- for(int i = 0; i < this.mask.length(); ++i) {
- char c = this.mask.charAt(i);
- switch (c) {
- case '#':
- case '&':
- case '9':
- case '?':
- case 'A':
- case 'a':
- this.maskEmpty[this.len] = this.getPromptChar();
- this.maskCompiled[this.len] = c;
- this.maskCase[this.len++] = caseState;
- break;
- case '<':
- caseState = 'L';
- break;
- case '>':
- caseState = 'U';
- break;
- case '\\':
- ++i;
- default:
- this.maskEmpty[this.len] = c;
- this.maskCompiled[this.len] = 'L';
- this.maskCase[this.len++] = '?';
- }
- }
-
- }
-
- public String getMask() {
- return new String(this.mask);
- }
-
- public void setMask(String mask) {
- this.mask = mask;
- this.maskCompile();
- }
-
- public int skipLiteral(int index, int direction) {
- if (index >= 0 && index < this.length()) {
- switch (direction) {
- case 1:
- while(index > 0 && this.maskCompiled[index] == 'L') {
- --index;
- }
- break;
- case 2:
- while(index < this.length() && this.maskCompiled[index] == 'L') {
- ++index;
- }
- }
- }
-
- return index;
- }
-
- public int length() {
- return this.len;
- }
- }
-